import GenerateKeys
import time
#对特征值进行处理，使特征值小于两个范围但是处理过后返回的两个值不可以相等
def changefingerrange(range1,range2):

    fingerprint1=range1
    fingerprint2=range2
    return fingerprint1 , fingerprint2


#定义求质数的函数
def getprim(n):
 #我们从3开始，提升效率，呵呵，微乎其微啦
    p=3
    x=0
    while(x<n):
        result=True
        for i in range(2,p-1):
            if(p%i==0):
                result=False
        if result==True:
            x=x+1
            rst=p
#注意:这里加2是为了提升效率，因为能被双数肯定不是质数。
        p+=2
    return rst
fingerprint=changefingerrange(10,15)
fingerprint1=fingerprint[0]
fingerprint2=fingerprint[1]
#调用函数
p_prime=0
p_prime=getprim(fingerprint1)
#print p_prime
q_prime=0
q_prime=getprim(fingerprint2)
#print q_prime

m =str(fingerprint1)+str(fingerprint2)
m1 = []
c = []
n = str(p_prime*q_prime)
u = ""
for i in range(0, len(m)):
    if (len(u) == len(n)-1):
        m1.append(int(u))
        u = m[i]
    else:
        u = u + m[i]
    if (i == len(m)-1):
        m1.append(int(u))

print(m1)

publicKey = []
privateKey = []

publicKey, privateKey = GenerateKeys.keyGenerator(p_prime, q_prime)

print("LLave: " + str(publicKey[0]))
t0=time.clock()
print("Encryption: ")

for i in range(0, len(m1)):
    z = GenerateKeys.powerMod(m1[i], publicKey[0], publicKey[1])
    print(z)
    c.append(z)
t1=time.clock()
print "ecryption time:"
print t1-t0



t2=time.clock()
print("Decryption: ")

for i in range(0, len(m1)):
    z = GenerateKeys.powerMod(c[i], privateKey[0], publicKey[1])
    print(z)
t3=time.clock()
print "decryption time:"
print t3-t2